﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Control.Bus;

namespace QuanLyKho
{
    public partial class FrmQuanLySP : Form
    {
        List<BusProduct> products = new List<BusProduct>();
        List<BusProduct> producttodelete = new List<BusProduct>();
        List<BusProduct> producttoupdate = new List<BusProduct>();
        bool isAddNew = new bool();
        bool isSaved = new bool();
        List<BusShelf> shelfs = new List<BusShelf>();
        public FrmQuanLySP()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmQuanLySP_Load(object sender, EventArgs e)
        {
            List<BusShelf> shelfs = BusShelf.GetAll();
            var groupshelfs = shelfs.GroupBy(sh => sh.ShelfName).OrderBy(sh => sh.Key)
                .Select(shs => new BusShelf()
                                    {
                                        ID=-1,
                                        ShelfName=shs.Key,
                                        Tray="0",
                                    }
                       );
            List<BusShelf> cbbdatasource = groupshelfs.ToList<BusShelf>();
            comboBox1.DataSource = cbbdatasource;
            comboBox1.DisplayMember = "ShelfName";
            comboBox1.ValueMember = "ID";
            products = BusProduct.GetAll();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = products;
            groupBox3.Enabled = false;

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                BusProduct p = products[dataGridView1.SelectedRows[0].Index];
                textBox1.Text = p.ProductCode;
                textBox2.Text = p.ProductName;
                comboBox1.Text = p.Shelf.ShelfName;
                comboBox2.Text = p.Shelf.Tray;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            string shelfname = comboBox1.Text;
            shelfs = BusShelf.GetListByShelfName(shelfname);
            comboBox2.DataSource = shelfs;
            comboBox2.DisplayMember = "Tray";
            comboBox2.ValueMember = "ID";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            isAddNew=true;
            groupBox3.Enabled = true;
            clearGroupbox3();
            button5.Enabled = false;
        }

        private void clearGroupbox3()
        {
            textBox1.Text = null;
            textBox2.Text = null;
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            isAddNew = false;
            groupBox3.Enabled = true;
            button6.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("bạn có muốn xóa không", "xác định", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                List<BusProduct> delete = new List<BusProduct>();
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    delete.Add(products[row.Index]);

                }
                foreach (BusProduct busDelete in delete)
                {
                    producttodelete.Add(busDelete);
                    products.Remove(busDelete);
                }
                bindingSource();
                IsnotSave();
            }
        }

        private void bindingSource()
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = products;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            groupBox3.Enabled = false;
            button5.Enabled = true;
            button6.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (isAddNew == true)
            {
                BusProduct newprod = new BusProduct()
                {
                    ID = -1,
                    Shelf = shelfs[comboBox2.SelectedIndex],
                    ProductCode = textBox1.Text,
                    ProductName = textBox2.Text,
                };
                products.Add(newprod);
                bindingSource();
            }
            else
            {
                BusShelf Shelf = shelfs[comboBox2.SelectedIndex];
                int ID=products[dataGridView1.SelectedRows[0].Index].ID;
                products[dataGridView1.SelectedRows[0].Index] = new BusProduct()
                {
                    ID = ID,
                    Shelf = Shelf,
                    ProductCode = textBox1.Text,
                    ProductName = textBox2.Text
                };
                BusProduct updateProd = products[dataGridView1.SelectedRows[0].Index];
                producttoupdate.Add(updateProd);
                bindingSource();
            }
            IsnotSave();
            
        }

        private void IsnotSave()
        {
            isSaved = false;
            button2.Text = "Lưu *";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (BusProduct item in products)
            {
                if (item.ID == -1)
                    item.Insert();
            }
            foreach (BusProduct item in producttoupdate)
            {
                item.Update();
            }
            foreach (BusProduct item in producttodelete)
            {
                item.Delete();
            }
            producttodelete = null;
            producttoupdate = null;
            isSaved = true;
            button2.Text = "Lưu";
        }

        private void button8_Click(object sender, EventArgs e)
        {
            products = BusProduct.Find(textBox4.Text);
            bindingSource();
        }
    }
}
